#!/usr/bin/env python
import os, hashlib, re

def findFiles(idirs, pattern, emptyDirs=None):
    out = []
    for idir in idirs:
        for root, dirs, files in os.walk(idir):
            curOut = []
            for x in files:
                m = pattern.match(x)
                if m is not None:
                    curOut.append(os.path.join(root,x))
            if emptyDirs is not None and len(curOut) == 0:
                emptyDirs[root] = True
            out += curOut 
    return out

def calcHash(fn):
    fp = open(fn, 'r')
    md5 = hashlib.md5(fp.read()).hexdigest()
    fp.close()
    return md5

def findEmptyParentDirs(emptyDirs, adir):
    bdir = os.path.dirname(adir)
    if len(bdir) == 0:
        return adir
    if not emptyDirs.has_key(bdir):
        return adir
    return findEmptyParentDirs(emptyDirs, bdir)

def main():
    pattern = re.compile('.*\.(jpg|png|jpeg|tif|tiff|png|bmp|mpg|mov|avi)$', re.I)
    emptyDirs = {}
    fns = findFiles(['/home/sphinx/Pictures'], pattern, emptyDirs)

    print emptyDirs

    albumNames = {}
    for fn in fns[0:10]:
        fdir = os.path.dirname(fn)
        tmpdir = findEmptyParentDirs(emptyDirs, fdir)
        albumName = os.path.basename(tmpdir)
        albumNames[albumName] = True

if __name__ == '__main__':
    main()
